<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="题库" prop="bankId">
        <el-select
          v-model="queryParams.bankId"
          placeholder="请选择题库"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="item in bankList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="章节" prop="chapter">
        <el-select
          v-model="queryParams.chapter"
          placeholder="请选择章节"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.EXAM_QUESTION_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="题干" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入题干"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label=" 阅卷类型" prop="markType">
        <el-select
          v-model="queryParams.markType"
          placeholder="请选择 阅卷类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.EXAM_MARK_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label=" 阅卷选项" prop="markOptions">
        <el-select
          v-model="queryParams.markOptions"
          placeholder="请选择 阅卷选项"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.EXAM_MARK_OPTIONS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="默认分值" prop="score">
        <el-input
          v-model="queryParams.score"
          placeholder="请输入默认分值"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select
          v-model="queryParams.state"
          placeholder="请选择 状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="难度" prop="difficulty">
        <el-select
          v-model="queryParams.difficulty"
          placeholder="请选择难度"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.EXAM_DIFFICULTY)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="知识点" prop="label">
        <el-input
          v-model="queryParams.label"
          placeholder="请输入知识点"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['exam:question:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['exam:question:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- 子表的列表 -->
      <el-table-column type="expand">
        <template #default="scope">
          <el-tabs model-value="questionOption">
            <el-tab-pane label="试题选项" name="questionOption">
              <QuestionOptionList :question-id="scope.row.id" />
            </el-tab-pane>
            <el-tab-pane label="试题答案" name="questionAnswer">
              <QuestionAnswerList :question-id="scope.row.id" />
            </el-tab-pane>
            <el-tab-pane label="知识点" name="questionOpLabel">
              <QuestionLabelList :question-id="scope.row.id" :label="scope.row.label"/>
            </el-tab-pane>
          </el-tabs>
        </template>
      </el-table-column>
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="题库" align="center" prop="bankId" />
      <el-table-column label="章节" align="center" prop="chapter" />
      <el-table-column label="题干" align="center" prop="title" />
      <el-table-column label="默认分值" align="center" prop="score" />
      <el-table-column label=" 阅卷类型" align="center" prop="markType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.EXAM_MARK_TYPE" :value="scope.row.markType" />
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.EXAM_QUESTION_TYPE" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label=" 阅卷选项" align="center" prop="markOptions">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.EXAM_MARK_OPTIONS" :value="scope.row.markOptions" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="难度" align="center" prop="difficulty">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.EXAM_DIFFICULTY" :value="scope.row.difficulty" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('preview', scope.row.id)"
            v-hasPermi="['exam:question:query']"
          >
            预览
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('detail', scope.row.id)"
            v-hasPermi="['exam:question:query']"
          >
            查看
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['exam:question:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['exam:question:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <!-- 表单弹窗：添加/修改 -->
  <QuestionShow ref="questionShowRef" @success="getList"/>

</template>

<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { QuestionApi, QuestionVO } from '@/api/exam/question'
import QuestionAnswerList from '@/views/exam/components/QuestionAnswerList.vue'
import QuestionOptionList from '@/views/exam/components/QuestionOptionList.vue'
import QuestionLabelList from '@/views/exam/components/QuestionLabelList.vue'
import {BankApi, BankVO} from "@/api/exam/bank";
const { push } = useRouter() // 路由跳转
/** 试题 列表 */
defineOptions({ name: 'Question' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<QuestionVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  bankId: undefined,
  type: undefined,
  title: undefined,
  markType: undefined,
  markOptions: undefined,
  score: undefined,
  state: undefined,
  difficulty: undefined,
  chapter: undefined,
  label: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await QuestionApi.getQuestionPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const questionShowRef = ref()
const openForm = (type: string, id?: number) => {
  // 修改
  if (typeof id === 'number') {
    if("detail" === type){
      push({ name: 'QuestionDetail', params: { id } })
      return;
    }else if("update" === type){
      push({name: 'QuestionEdit', params: {id}})
    } else if("preview" === type){
      //
      questionShowRef.value.open(id)
    }
    return
  }
  // 新增
  push({ name: 'QuestionAdd' })
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await QuestionApi.deleteQuestion(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await QuestionApi.exportQuestion(queryParams)
    download.excel(data, '试题.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const bankList = ref<BankVO[]>([]) // 列表的数据

const getBankList = async () => {
  loading.value = true
  try {
    const data = await BankApi.getBankPage({})
    bankList.value = data.list
  } finally {
    loading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getBankList();
  getList()
})
</script>
